﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace GTA
{
    public partial class FrmReportTaskHistoryChangeRequest : BaseForm
    {
        #region Constants
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_GENERAL = "Relatório Histórico de Interrupção - ChangeRequest - Por Motivo";
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_ARCHITECTS = "Relatório Histórico de Interrupção - ChangeRequest - Por Motivo e Arquiteto";
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_PROJECTS = "Relatório Histórico de Interrupção - ChangeRequest - Por Motivo e Projeto";
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_GENERAL = "Relatório Histórico de Interrupção - ChangeRequest - Por Resultado";
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_ARCHITECTS = "Relatório Histórico de Interrupção - ChangeRequest - Por Resultado e Arquiteto";
        private const String REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_PROJECTS = "Relatório Histórico de Interrupção - ChangeRequest - Por Resultado e Projeto";
        #endregion

        #region Starting Form
        public FrmReportTaskHistoryChangeRequest()
        {
            InitializeComponent();
        }
        private void FrmReportTaskHistory_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                CleanFilter();
            }
            catch (Exception ex)
            {
                HandleException("Inicialização da tela", ex);
            }
        }
        #endregion

        #region Events
        private void cmdGenerateReport_Click(object sender, EventArgs e)
        {
            GenerateReport();
        }
        private void cmdCleanFilter_Click(object sender, EventArgs e)
        {
            CleanFilter();
        }
        #endregion

        #region Methods
        public void CleanFilter()
        {
            ucTaskHistoryReportFilter1.CleanFilter();
            opTypeTasksMotive.Checked = true;
            opGroupGeneral.Checked = true;
        }
        private void GenerateReport()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                BusinessEntity.TaskHistoryFilter filter = ucTaskHistoryReportFilter1.GetReportFilter();
                String filterDescription = GetFilterDescription();
                List<BusinessEntity.TaskHistoryChangeRequestReport> historyList = null;
                FrmReportViewer wd = null;
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                if (opTypeTasksMotive.Checked) // Task History Report - Change Request - by Change Request Motivation
                {
                    historyList = bl.ListTaskHistoryChangeRequestMotivationReport(filter);
                    if (opGroupGeneral.Checked) // General Visualization - Without groupping
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestMotivationGeneral reportMG = new GTA.Files.Reports.ReportTaskHistoryChangeRequestMotivationGeneral();
                        reportMG.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportMG);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_GENERAL;
                        reportMG.SetParameterValue("param_filter", filterDescription);
                    }
                    else if (opGroupArchitect.Checked) // Groupping by Architects
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestMotivationArchitect reportMA = new GTA.Files.Reports.ReportTaskHistoryChangeRequestMotivationArchitect();
                        reportMA.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportMA);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_ARCHITECTS;
                        reportMA.SetParameterValue("param_filter", filterDescription);
                    }
                    else if (opGroupProject.Checked) // Groupping by Projects
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestMotivationProject reportMP = new GTA.Files.Reports.ReportTaskHistoryChangeRequestMotivationProject();
                        reportMP.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportMP);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_MOTIVATION_PROJECTS;
                        reportMP.SetParameterValue("param_filter", filterDescription);
                    }
                }
                else if (opTypeTasksResult.Checked) // Task History Report - Change Request - by Change Request Results
                {
                    historyList = bl.ListTaskHistoryChangeRequestResultReport(filter);
                    if (opGroupGeneral.Checked) // General Visualization - Without groupping
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestResultGeneral reportRG = new GTA.Files.Reports.ReportTaskHistoryChangeRequestResultGeneral();
                        reportRG.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportRG);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_GENERAL;
                        reportRG.SetParameterValue("param_filter", filterDescription);
                    }
                    else if (opGroupProject.Checked) // Groupping by Projects
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestResultProjects reportRP = new GTA.Files.Reports.ReportTaskHistoryChangeRequestResultProjects();
                        reportRP.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportRP);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_PROJECTS;
                        reportRP.SetParameterValue("param_filter", filterDescription);
                    }
                    else if (opGroupArchitect.Checked) // Groupping by Architects
                    {
                        Files.Reports.ReportTaskHistoryChangeRequestResultArchitects reportRA = new GTA.Files.Reports.ReportTaskHistoryChangeRequestResultArchitects();
                        reportRA.SetDataSource(historyList);
                        wd = new FrmReportViewer(reportRA);
                        wd.Text = REPORT_TASK_HISTORY_CHANGE_REQUEST_RESULT_ARCHITECTS;
                        reportRA.SetParameterValue("param_filter", filterDescription);
                    }
                }

                if (wd != null)
                {
                    wd.MdiParent = this.MdiParent;
                    wd.Show();
                }
            }
            catch (Exception ex)
            {
                HandleException("Gerar o relatório", ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private string GetFilterDescription()
        {
            String filterDescription = String.Empty;

            filterDescription += ucTaskHistoryReportFilter1.GetFilterDescription();

            if (String.IsNullOrEmpty(filterDescription))
                filterDescription = "Nenhum";

            return filterDescription;
        }
        #endregion
    }
}
